home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / CH2 / FruitLoopy.cs < prev    next >
Text File  |  2006-09-11  |  2KB  |  72 lines

  1. // ========================================================================
  2. //  FruitLoopy.cs
  3. //
  4. //  This program adds up the costs and quantities of selected fruit types
  5. //  and outputs the results to the display. This module is a variation
  6. //  of the the Fruit.cs module
  7. // ========================================================================
  8.  
  9. function runFruitLoopy()
  10. // ----------------------------------------------------
  11. //     Entry point for the program.
  12. // ----------------------------------------------------
  13. {
  14.    //
  15.    // ----------------- Initialization ---------------------
  16.    //
  17.  
  18.    %numFruitTypes = 5; // so we know how many types are in our arrays
  19.  
  20.    %bananaIdx=0;    // initilize the values of our index variables
  21.    %appleIdx=1;
  22.    %orangeIdx=2;
  23.    %mangoIdx=3;
  24.    %pearIdx=4;
  25.  
  26.    %names[%bananaIdx] = "bananas"; // initilize the fruit name values
  27.    %names[%appleIdx] = "apples";
  28.    %names[%orangeIdx] = "oranges";
  29.    %names[%mangoIdx] = "mangos";
  30.    %names[%pearIdx] = "pears";
  31.  
  32.    %cost[%bananaIdx] = 1.15; // initilize the price values
  33.    %cost[%appleIdx] = 0.55;
  34.    %cost[%orangeIdx] = 0.55;
  35.    %cost[%mangoIdx] = 1.90;
  36.    %cost[%pearIdx] = 0.68;
  37.  
  38.    %quantity[%bananaIdx] = 1; // initilize the quantity values
  39.    %quantity[%appleIdx]  = 3;
  40.    %quantity[%orangeIdx] = 4;
  41.    %quantity[%mangoIdx]  = 1;
  42.    %quantity[%pearIdx]   = 2;
  43.  
  44.    %numFruit=0;     // always a good idea to initialize *all* variables!
  45.    %totalCost=0;    // (even if we know we are going to change them later)
  46.  
  47.    //
  48.    // ----------------- Computation ---------------------
  49.    //
  50.  
  51.    // Display the known statistics of the fruit collection
  52.    for (%index = 0; %index < %numFruitTypes; %index++)
  53.    {
  54.    echo("Cost of " @ %names[%index] @ ":$" @ %cost[%index]);
  55.    echo("Number of " @ %names[%index] @ ":" @ %quantity[%index]);
  56.    }
  57.  
  58.    // count up all the pieces of fruit, and display that result
  59.    for (%index = 0; %index < %numFruitTypes; %index++)
  60.    {
  61.    %numFruit = %numFruit + %quantity[%index];
  62.    }
  63.    echo("Total pieces of Fruit:" @ %numFruit);
  64.  
  65.  
  66.    // now calculate the total cost
  67.    for (%index = 0; %index < %numFruitTypes; %index++)
  68.    {
  69.    %totalCost = %totalCost + (%quantity[%index]*%cost[%index]);
  70.    }
  71.    echo("Total Price of Fruit:$" @ %totalCost);
  72. }